#include "DialogReadHistory.h"

//(*InternalHeaders(DialogReadHistory)
#include <wx/intl.h>
#include <wx/string.h>
//*)

#include "GlobalParam.h"
#include "Typedef.h"
#include "History.h"

//(*IdInit(DialogReadHistory)
const long DialogReadHistory::ID_LISTCTRL1 = wxNewId();
const long DialogReadHistory::ID_DATEPICKERCTRL1 = wxNewId();
const long DialogReadHistory::ID_TEXTCTRL1 = wxNewId();
const long DialogReadHistory::ID_BUTTON1 = wxNewId();
const long DialogReadHistory::ID_BUTTON2 = wxNewId();
const long DialogReadHistory::ID_BUTTON3 = wxNewId();
const long DialogReadHistory::ID_BUTTON4 = wxNewId();
//*)

BEGIN_EVENT_TABLE(DialogReadHistory,wxDialog)
	//(*EventTable(DialogReadHistory)
	//*)
END_EVENT_TABLE()

DialogReadHistory::DialogReadHistory(wxWindow* parent,Action* action,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(DialogReadHistory)
	Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
	SetClientSize(wxSize(365,457));
	Move(wxDefaultPosition);
	ListCtrl1 = new wxListCtrl(this, ID_LISTCTRL1, wxPoint(16,24), wxSize(320,264), wxLC_REPORT|wxLC_EDIT_LABELS|wxSUNKEN_BORDER|wxVSCROLL, wxDefaultValidator, _T("ID_LISTCTRL1"));
	DatePickerCtrl1 = new wxDatePickerCtrl(this, ID_DATEPICKERCTRL1, wxDefaultDateTime, wxPoint(24,328), wxSize(200,28), wxDP_DEFAULT|wxDP_SHOWCENTURY, wxDefaultValidator, _T("ID_DATEPICKERCTRL1"));
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxPoint(240,328), wxSize(80,27), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	Button1 = new wxButton(this, ID_BUTTON1, _("提交"), wxPoint(16,392), wxSize(72,32), 0, wxDefaultValidator, _T("ID_BUTTON1"));
	Button2 = new wxButton(this, ID_BUTTON2, _("关闭"), wxPoint(256,392), wxSize(72,32), 0, wxDefaultValidator, _T("ID_BUTTON2"));
	Button3 = new wxButton(this, ID_BUTTON3, _("删除"), wxPoint(96,392), wxSize(72,32), 0, wxDefaultValidator, _T("ID_BUTTON3"));
	Button4 = new wxButton(this, ID_BUTTON4, _("刷新"), wxPoint(176,392), wxSize(72,32), 0, wxDefaultValidator, _T("ID_BUTTON4"));

	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogReadHistory::OnAddHistory);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogReadHistory::OnClose);
	Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogReadHistory::OnDeleteHistory);
	Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogReadHistory::OnRefresh);
	//*)

	ThisAction = action;
    BookID = -1;

    InitListCtrl();
}

DialogReadHistory::~DialogReadHistory()
{
	//(*Destroy(DialogReadHistory)
	//*)
}


void DialogReadHistory::OnAddHistory(wxCommandEvent& event)
{
    wxDateTime Date;
    wxString StrDateTime;
    wxString StrFormat = wxString::FromUTF8("%Y-%m-%d");
    wxString StrPage;
    History* NewHistory = new History();
    BPcode rv;
    bool success;
    long page;

    Date = DatePickerCtrl1->GetValue();
    StrDateTime = Date.Format(StrFormat);
    StrPage = TextCtrl1->GetValue();

    success = StrPage.ToLong(&page,10);
    if(!success)
    {
        wxString title = wxString::FromUTF8("fail");
        wxString confirm = wxString::FromUTF8("fail to add this history");
        wxMessageBox(title,confirm,wxOK,this);
        bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:fail to add this history",page);
        return;
    }

    NewHistory->ReadDate = (char *)StrDateTime.char_str();
    NewHistory->ReadPage = page;

    rv = ThisAction->AddHisotryIntoTheBookNPool(BookID,NewHistory);
    if(rv != BP_OK)
    {
        delete NewHistory;

        wxString title = wxString::FromUTF8("fail");
        wxString confirm = wxString::FromUTF8("fail to add the new history");
        wxMessageBox(title,confirm,wxOK,this);
        bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:fail to add the new history");
    }

    RefreshHistoryList(true);
}

void DialogReadHistory::OnClose(wxCommandEvent& event)
{
    this->Show(false);
}

void DialogReadHistory::OnDeleteHistory(wxCommandEvent& event)
{
    long index = -1;
    wxString StrHistoryID;
    long HID;
    bool success;
    BPcode rv;

    index = ListCtrl1->GetNextItem(index,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
    if(index == -1)
    {
        wxString title = wxString::FromUTF8("Please select at least on row");
        wxString confirm = wxString::FromUTF8("OK");
        wxMessageBox(title,confirm,wxOK,this);
        return;
    }

    bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:index is %d",index);

    StrHistoryID = ListCtrl1->GetItemText(index);
    success = StrHistoryID.ToLong(&HID,10);
    if(!success)
    {
        wxString title = wxString::FromUTF8("fail");
        wxString confirm = wxString::FromUTF8("fail to delete this history");
        wxMessageBox(title,confirm,wxOK,this);
        bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:fail to delete %d history",HID);
        return;
    }

    bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:History ID by which to delete history is %s",StrHistoryID.c_str());

    rv = ThisAction->DeleteHistoryFromBookNPool(BookID,HID);
    if(rv != BP_OK)
    {
        wxString title = wxString::FromUTF8("fail");
        wxString confirm = wxString::FromUTF8("fail to delete this history");
        wxMessageBox(title,confirm,wxOK,this);
        bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:fail to delete %d history",HID);
        return;
    }

    RefreshHistoryList(true);
}

void DialogReadHistory::RefreshHistoryList(Bbool UpdateOnly)
{
    Book* ThisBook = NULL;
    list<History*>* HList;
    list<History*>::iterator Itor;

    ListCtrl1->DeleteAllItems();

    if(UpdateOnly)
        ThisBook = ThisAction->RefreshHistorysFromDatabase(BookID);
    else
        ThisBook = ThisAction->GetBookAndHistorysFromPoolOrInsert(BookID);
    if(!ThisBook)
    {
        bplog(LOG_LEVEL_ERROR,"DialogReadHistory:GetBookAndHistorysFromPoolOrInsert(%d) returns NULL",BookID);
        return;
    }

    HList = ThisBook->GetHistorysPtr();
    int i = 1;
    for(Itor=HList->begin(); Itor!=HList->end(); Itor++,i++)
    {
        wxString buf;
        long tmp = 0;
        int id = ((History *)(*Itor))->HistoryID;
        int page = ((History *)(*Itor))->ReadPage;
        string date = ((History *)(*Itor))->ReadDate;

        buf.Printf(_T("%d"),id);
        //bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:now insert item %d, column 0, text:%s",i,(char *)buf.char_str());
        tmp = ListCtrl1->InsertItem(i, buf, 0);
        ListCtrl1->SetItemData(tmp, i);

        buf.Printf(_T("%d"),page);
        //bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:now insert item %d, column 1, text:%s",i,(char *)buf.char_str());
        ListCtrl1->SetItem(tmp, 1, buf);

        buf = wxString(date.c_str(), wxConvUTF8);
        //bplog(LOG_LEVEL_DEBUG,"DialogReadHistory:now insert item %d, column 2, text:%s",i,(char *)buf.char_str());
        ListCtrl1->SetItem(tmp, 2, buf);
    }
}

void DialogReadHistory::InitListCtrl()
{
    int shift = 0;

    wxListItem itemCol;

    itemCol.SetText(_T("记录ID"));
    itemCol.SetImage(-1);
    ListCtrl1->InsertColumn(0, itemCol);

    itemCol.SetText(_T("页数"));
    itemCol.SetImage(-1);
    ListCtrl1->InsertColumn(1, itemCol);

    itemCol.SetText(_T("日期"));
    itemCol.SetImage(-1);
    ListCtrl1->InsertColumn(2, itemCol);

    ListCtrl1->SetColumnWidth(0, 70);
    ListCtrl1->SetColumnWidth(1, 50);
    ListCtrl1->SetColumnWidth(2, 200);
}

void DialogReadHistory::SetBookID(int BookID)
{
    this->BookID = BookID;
}

int DialogReadHistory::GetBookID()
{
    return BookID;
}

void DialogReadHistory::OnRefresh(wxCommandEvent& event)
{
    RefreshHistoryList(true);
}
